package com.ruoyi.wb.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wb.domain.WbFireServiceUnit;
import com.ruoyi.wb.domain.WbFireServiceUser;
import com.ruoyi.wb.dto.WbFireServiceUserDto;
import com.ruoyi.wb.enmus.RoleNameEnums;
import com.ruoyi.wb.mapper.WbFireServiceUnitMapper;
import com.ruoyi.wb.mapper.WbFireServiceUserMapper;
import com.ruoyi.wb.service.IWbFireServiceUserService;
import com.ruoyi.wb.vo.WbFireServiceUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 消防用户Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-06
 */
@Service
public class WbFireServiceUserServiceImpl implements IWbFireServiceUserService
{
    @Autowired
    private WbFireServiceUserMapper wbFireServiceUserMapper;

    @Autowired
    private WbFireServiceUnitMapper wbFireServiceUnitMapper;

@Autowired
private ISysUserService sysUserService;

    /**
     * 查询消防用户
     * 
     * @param id 消防用户主键
     * @return 消防用户
     */
    @Override
    public WbFireServiceUser selectWbFireServiceUserById(Long id)
    {
        return wbFireServiceUserMapper.selectWbFireServiceUserById(id);
    }

    /**
     * 查询消防用户列表
     * 
     * @param wbFireServiceUser 消防用户
     * @return 消防用户
     */
    @Override
    public List<WbFireServiceUser> selectWbFireServiceUserList(WbFireServiceUser wbFireServiceUser)
    {
        return wbFireServiceUserMapper.selectWbFireServiceUserList(wbFireServiceUser);
    }

    /**
     * 新增消防用户
     * 
     * @param wbFireServiceUser 消防用户
     * @return 结果
     */
    @Override
    public int insertWbFireServiceUser(WbFireServiceUser wbFireServiceUser)
    {
        wbFireServiceUser.setCreateTime(DateUtils.getNowDate());
        return wbFireServiceUserMapper.insertWbFireServiceUser(wbFireServiceUser);
    }

    /**
     * 修改消防用户
     * 
     * @param wbFireServiceUser 消防用户
     * @return 结果
     */
    @Override
    public int updateWbFireServiceUser(WbFireServiceUser wbFireServiceUser)
    {
        wbFireServiceUser.setUpdateTime(DateUtils.getNowDate());
        return wbFireServiceUserMapper.updateWbFireServiceUser(wbFireServiceUser);
    }

    /**
     * 批量删除消防用户
     * 
     * @param ids 需要删除的消防用户主键
     * @return 结果
     */
    @Override
    public int deleteWbFireServiceUserByIds(Long[] ids)
    {
        sysUserService.deleteUserByIds(ids);
        return wbFireServiceUserMapper.deleteWbFireServiceUserByUserIds(ids);
    }

    /**
     * 删除消防用户信息
     * 
     * @param id 消防用户主键
     * @return 结果
     */
    @Override
    public int deleteWbFireServiceUserById(Long id)
    {
        return wbFireServiceUserMapper.deleteWbFireServiceUserById(id);
    }

    @Override
    public List<WbFireServiceUserVo> listByWbFireServiceUserDto(WbFireServiceUserDto wbFireServiceUserDto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();

        WbFireServiceUnit wbFireServiceUnit = wbFireServiceUnitMapper.getFireServiceUnitByUserId(loginUser.getUserId());
       if (!ObjectUtils.isEmpty(wbFireServiceUnit)){
           wbFireServiceUserDto.setUnitId(wbFireServiceUnit.getId());
       }
        return  wbFireServiceUnitMapper.listByWbFireServiceUserDto(wbFireServiceUserDto);
    }


    /**
     * 新增消防用户
     */
    @Transactional
    @Override
    public int addByWbFireServiceUserDto(WbFireServiceUserDto wbFireServiceUserDto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //查询当前角色消防机构
//      WbFireServiceUnit wbFireServiceUnit = wbFireServiceUnitMapper.getFireServiceUnitByUserId(loginUser.getUserId());
//        if (ObjectUtils.isEmpty(wbFireServiceUnit)){
//            throw new RuntimeException("当前用户没有权限");
//        }

        SysUser sysUser = wbFireServiceUserDto.getSysUser();
        sysUser.setRoleId(Long.valueOf(RoleNameEnums.FIREFIGHTER.getType()));
        //新增用户 和 角色
        boolean b = sysUserService.checkUserNameUnique(sysUser);
        if (!b){
            throw new RuntimeException("用户名已存在");
        }
        boolean p = sysUserService.checkPhoneUnique(sysUser);
        if (!p){
            throw new RuntimeException("手机号已存在");
        }
        if (StringUtils.isEmpty(sysUser.getPassword())||sysUser.getPassword().length()<5){
            throw new RuntimeException("密码长度必须大于5");
        }
        sysUser.setPassword(SecurityUtils.encryptPassword(sysUser.getPassword()));
        int i = sysUserService.insertUser(sysUser);
        if (i==0){
            throw new RuntimeException("新增用户失败");
        }
        //新增 消防单位 和用户
        WbFireServiceUser wbFireServiceUser = new WbFireServiceUser();
        wbFireServiceUser.setUserId(sysUser.getUserId());
        wbFireServiceUser.setFireServiceUnitId(wbFireServiceUserDto.getUnitId());
        int i1 = wbFireServiceUserMapper.insertWbFireServiceUser(wbFireServiceUser);
        if (i1 == 0) {
            throw new RuntimeException("新增用户失败");
        }
        return i;
    }

    /**
     * 修改消防用户
     */
    @Override
    public int editByWbFireServiceUserDto(WbFireServiceUserDto wbFireServiceUserDto) {
        SysUser sysUser = wbFireServiceUserDto.getSysUser();
        WbFireServiceUser wbFireServiceUser = new WbFireServiceUser();
        wbFireServiceUser.setUserId(sysUser.getUserId());
        List<WbFireServiceUser> wbFireServiceUsers = selectWbFireServiceUserList(wbFireServiceUser);
        if (!CollectionUtils.isEmpty(wbFireServiceUsers)){
            wbFireServiceUser.setFireServiceUnitId(wbFireServiceUserDto.getUnitId());
            wbFireServiceUser.setId(wbFireServiceUsers.get(0).getId());
            wbFireServiceUserMapper.updateWbFireServiceUser(wbFireServiceUser);
        }
       return sysUserService.updateUser(sysUser);
    }

    /**
     * 获取消防用户详细信息
     */
    @Override
    public WbFireServiceUserVo byUserId(Long userId) {
        WbFireServiceUserDto wbFireServiceUserDto = new WbFireServiceUserDto();
        wbFireServiceUserDto.setUserId(userId);
        List<WbFireServiceUserVo> wbFireServiceUserVos = wbFireServiceUnitMapper.listByWbFireServiceUserDto(wbFireServiceUserDto);
        if (!CollectionUtils.isEmpty(wbFireServiceUserVos)){
            return wbFireServiceUserVos.get(0);
        }
        return null;
    }
}
